<?php
// -------------- 用户相关函数 ----------------

/**
 * 检测是否登陆
 * 
 * @param string $uname
 * @param string $urnd
 * @return Ambigous <string, unknown>
 */
function islogin($uname = '', $urnd = '') {
	global $set_username, $set_outtime;
	$username = $uname ? $uname : getcvar ( 'bakusername' );
	$rnd = $urnd ? $urnd : getcvar ( 'bakrnd' );
	if (empty ( $username ) || empty ( $rnd )) {
		printerror ( "NotLogin", "index.php" );
	}
	if ($username != $set_username) {
		printerror ( "NotLogin", "index.php" );
	}
	CHCookieRnd ( $username, $rnd );
	$time = time ();
	if ($time - getcvar ( 'baklogintime' ) > $set_outtime * 60) {
		printerror ( "OutLogintime", "index.php" );
	}
	esetcookie ( "baklogintime", $time, 0 );
	$lr ['username'] = $username;
	$lr ['rnd'] = $rnd;
	return $lr;
}

/**
 * 设置COOKIE认证
 * 
 * @param unknown $username
 * @param unknown $rnd
 */
function SCookieRnd($username, $rnd) {
	global $set_loginrnd;
	$ckpass = md5 ( md5 ( $rnd . $set_loginrnd ) . '-' . $rnd . '-' . $username . '-' );
	esetcookie ( "loginebakckpass", $ckpass, 0 );
}

/**
 * 验证COOKIE认证
 * 
 * @param unknown $username
 * @param unknown $rnd
 */
function CHCookieRnd($username, $rnd) {
	global $set_loginrnd;
	$ckpass = md5 ( md5 ( $rnd . $set_loginrnd ) . '-' . $rnd . '-' . $username . '-' );
	if ($ckpass != getcvar ( 'loginebakckpass' )) {
		printerror ( "NotLogin", "index.php" );
	}
}

/**
 * 退出系统
 */
function LoginOut() {
	esetcookie ( "bakusername", "", 0 );
	esetcookie ( "bakrnd", "", 0 );
	esetcookie ( "loginebakckpass", "", 0 );
	esetcookie ( "baklogintime", "", 0 );
	printerror ( "ExitSuccess", "index.php" );
}

/**
 * 登陆系统
 * 
 * @param unknown $lusername
 * @param unknown $lpassword
 * @param unknown $key
 * @param number $lifetime
 */
function login($lusername, $lpassword, $key, $lifetime = 0) {
	global $set_username, $set_password, $set_loginauth, $set_loginkey;
	
	// 账号密码为空
	if (empty ( $lusername ) || empty ( $lpassword )) {
		printerror ( "EmptyLoginUser", "index.php" );
	}
	// 验证码
	if (! $set_loginkey) {
		if ($key != getcvar ( 'checkkey' ) || empty ( $key )) {
			printerror ( "FailLoginKey", "index.php" );
		}
	}
	if (md5 ( $lusername ) != md5 ( $set_username ) || md5 ( $lpassword ) != $set_password) {
		printerror ( "ErrorUser", "index.php" );
	}
	// 认证码
	if ($set_loginauth && $set_loginauth != $_POST ['loginauth']) {
		printerror ( "ErrorLoginAuth", "index.php" );
	}
	$logintime = time ();
	$rnd = make_password ( 12 );
	$s1 = esetcookie ( "bakusername", $lusername, 0 );
	$s2 = esetcookie ( "bakrnd", $rnd, 0 );
	$s3 = esetcookie ( "baklogintime", $logintime, 0 );
	SCookieRnd ( $lusername, $rnd );
	if (! $s1 || ! $s2) {
		printerror ( "NotOpenCookie", "index.php" );
	}
	printerror ( "LoginSuccess", "admin.php" );
}

// -------------- 公用函数 ----------------

/**
 * 提示并跳转
 * 
 * @param string $error
 * @param string $gotourl
 * @param number $ecms
 */
function printerror($error = "", $gotourl = "", $ecms = 0) {
	global $dblink, $public_r, $editor;
	if ($editor == 1) {
		$a = "../";
	} elseif ($editor == 2) {
		$a = "../../";
	} elseif ($editor == 3) {
		$a = "../../../";
	} else {
		$a = "";
	}
	if (strstr ( $gotourl, "(" ) || empty ( $gotourl )) {
		$gotourl_js = "history.go(-1)";
		$gotourl = "javascript:history.go(-1)";
	} else {
		$gotourl_js = "self.location.href='$gotourl';";
	}
	if (empty ( $error )) {
		$error = "DbError";
	}
	if ($ecms == 0) {
		@include $a . LoadLang ( "m.php" );
		$error = $message_r [$error];
		@include $a . LoadAdminTemp ( 'message.php' );
	} elseif ($ecms == 9) {
		// 弹出对话框
		@include $a . LoadLang ( "m.php" );
		$error = $message_r [$error];
		echo "<script>alert('" . $error . "');" . $gotourl_js . "</script>";
	}
	exit ();
}

/**
 * 字符截取
 * 
 * @param unknown $title
 * @param unknown $lengh
 * @return string
 */
function sub_str($title, $lengh) {
	if (strlen ( $title ) > $lengh) {
		$pp = 2;
		$len = strlen ( $title );
		if ($len % 2 != 0) {
			$pp = 1;
		}
		$title = substr ( $title, 0, $lengh - $pp );
		$title = $title . ' …';
	}
	return $title;
}

/**
 * 取得文件扩展名
 * 
 * @param unknown $filename
 * @return string
 */
function GetFiletype($filename) {
	$filer = explode ( ".", $filename );
	$count = count ( $filer ) - 1;
	return strtolower ( "." . $filer [$count] );
}

/**
 * 字符截取函数2
 * 
 * @param unknown $Modi_Str
 * @param unknown $start
 * @param unknown $length
 * @param string $mode
 * @return string
 */
function sub($Modi_Str, $start, $length, $mode = false) {
	$n = 0;
	for($i = 0; $i < $start; $i ++) {
		if (ord ( substr ( $Modi_Str, $i, 1 ) ) > 0xa0) {
			if ($mode) {
				$start ++;
				$i ++;
			}
			$n ++;
		}
	}
	if (! $mode)
		$start = $start + $n % 2;
	$The_length = $start + $length;
	for($i = $start; $i < $The_length; $i ++) {
		if (ord ( substr ( $Modi_Str, $i, 1 ) ) > 0xa0) {
			$The_Str .= substr ( $Modi_Str, $i, 2 );
			$i ++;
			if ($mode)
				$The_length ++;
		} else {
			$The_Str .= substr ( $Modi_Str, $i, 1 );
		}
	}
	return $The_Str;
}

/**
 * 取得随机数
 * 
 * @param int $pw_length 随机数长度
 * @return string
 */
function make_password($pw_length) {
	$low_ascii_bound = 50;
	$upper_ascii_bound = 122;
	$notuse = array (
			58,
			59,
			60,
			61,
			62,
			63,
			64,
			73,
			79,
			91,
			92,
			93,
			94,
			95,
			96,
			108,
			111 
	);
	$i = 0;
	$password1 = '';
	while ( $i < $pw_length ) {
		mt_srand ( ( double ) microtime () * 1000000 );
		$randnum = mt_rand ( $low_ascii_bound, $upper_ascii_bound );
		if (! in_array ( $randnum, $notuse )) {
			$password1 = $password1 . chr ( $randnum );
			$i ++;
		}
	}
	return $password1;
}

/**
 * 删除文件
 * 
 * @param unknown $filename
 */
function DelFiletext($filename) {
	@unlink ( $filename );
}

/**
 * 取得文件内容
 * 
 * @param unknown $filepath
 * @return unknown
 */
function ReadFiletext($filepath) {
	$htmlfp = @fopen ( $filepath, "r" );
	$string = '';
	while ( ! ! $data = @fread ( $htmlfp, 1000 ) ) {
		$string .= $data;
	}
	@fclose ( $htmlfp );
	return $string;
}

/**
 * 写文件
 * 
 * @param unknown $filepath
 * @param unknown $string
 */
function WriteFiletext($filepath, $string) {
	global $filechmod;
	$string = stripSlashes ( $string );
	$fp = @fopen ( $filepath, "w" );
	@fputs ( $fp, $string );
	@fclose ( $fp );
	if (empty ( $filechmod )) {
		@chmod ( $filepath, 0777 );
	}
}

/**
 * 写文件
 * 
 * @param unknown $filepath
 * @param unknown $string
 */
function WriteFiletext_n($filepath, $string) {
	global $filechmod;
	$fp = @fopen ( $filepath, "w" );
	@fputs ( $fp, $string );
	@fclose ( $fp );
	if (empty ( $filechmod )) {
		@chmod ( $filepath, 0777 );
	}
}

/**
 * 建立目录函数
 * 
 * @param unknown $path
 * @return boolean
 */
function DoMkdir($path) {
	global $public_r;
	// 不存在则建立
	if (! file_exists ( $path )) {
		// 安全模式
		if ($public_r [phpmode]) {
			$pr [0] = $path;
			FtpMkdir ( $ftpid, $pr );
			$mk = 1;
		} else {
			$mk = @mkdir ( $path, 0777 );
		}
		@chmod ( $path, 0777 );
		if (empty ( $mk )) {
			printerror ( "NotMkdir", "history.go(-1)" );
		}
	}
	return true;
}

/**
 * 替换目录值
 * 
 * @param unknown $path
 * @return mixed
 */
function RepPathStr($path) {
	$path = str_replace ( "\\", "", $path );
	$path = str_replace ( "/", "", $path );
	return $path;
}

/**
 * 时间转换
 * 
 * @param unknown $time
 * @return string
 */
function ToChangeUseTime($time) {
	global $fun_r;
	$usetime = time () - $time;
	if ($usetime < 60) {
		$tstr = $usetime . $fun_r ['TimeSecond'];
	} else {
		$usetime = round ( $usetime / 60 );
		$tstr = $usetime . $fun_r ['TimeMinute'];
	}
	return $tstr;
}

/**
 * 转换大小
 * 
 * @param unknown $size
 * @return string
 */
function ChangeSize($size) {
	if ($size < 1024) {
		$str = $size . " B";
	} elseif ($size < 1024 * 1024) {
		$str = round ( $size / 1024, 2 ) . " KB";
	} elseif ($size < 1024 * 1024 * 1024) {
		$str = round ( $size / (1024 * 1024), 2 ) . " MB";
	} else {
		$str = round ( $size / (1024 * 1024 * 1024), 2 ) . " GB";
	}
	return $str;
}

/**
 * 删除目录函数
 * 
 * @param unknown $DelPath
 * @return boolean
 */
function DelPath($DelPath) {
	include_once 'include/delpath.php';
	$wm_chief = new del_path ();
	$wm_chief_ok = $wm_chief->wm_chief_delpath ( $DelPath );
	return $wm_chief_ok;
}

/**
 * 打包目录
 * 
 * @param unknown $path
 * @param unknown $zipname
 */
function ZipFile($path, $zipname) {
	global $bakpath, $bakzippath;
	include_once 'include/phpzip.inc.php';
	$z = new PHPZip ();
	$z->Zip ( $bakpath . "/" . $path, $bakzippath . "/" . $zipname ); // 添加指定目录
}

/**
 * 压缩目录
 * 
 * @param unknown $path
 */
function Dozip($path) {
	global $bakpath, $bakzippath;
	if (strstr ( $path, ".." )) {
		printerror ( "DelPathNotExists", "history.go(-1)", 9 );
	}
	if (empty ( $path )) {
		printerror ( "DelPathNotExists", "history.go(-1)", 9 );
	}
	$mypath = $bakpath . "/" . $path;
	if (! file_exists ( $mypath )) {
		printerror ( "DelPathNotExists", "history.go(-1)", 9 );
	}
	$zipname = $path . ".zip";
	ZipFile ( $path, $zipname );
	echo "<script>self.location.href='down_zip.php?f=$zipname&p=$path';</script>";
}

/**
 * 去除adds
 * 
 * @param unknown $data
 * @return string
 */
function ClearAddsData($data) {
	$magic_quotes_gpc = @get_magic_quotes_gpc ();
	if ($magic_quotes_gpc) {
		$data = stripSlashes ( $data );
	}
	return $data;
}

// -------------- 数据库函数 ----------------

// --------------备份
/**
 * 初使化备份
 * 
 * @param unknown $add
 */
function DoEbak($add) {
	global $dblink, $bakpath, $fun_r, $db_version;
	$dbname = RepPostVar ( $add ['mydbname'] );
	if (empty ( $dbname )) {
		printerror ( "NotChangeDb", "history.go(-1)" );
	}
	$tablename = $add ['tablename'];
	$count = count ( $tablename );
	if (empty ( $count )) {
		printerror ( "EmptyChangeTb", "history.go(-1)" );
	}
	$add ['baktype'] = ( int ) $add ['baktype'];
	$add ['filesize'] = ( int ) $add ['filesize'];
	$add ['bakline'] = ( int ) $add ['bakline'];
	$add ['autoauf'] = ( int ) $add ['autoauf'];
	if ((! $add ['filesize'] && ! $add ['baktype']) || (! $add ['bakline'] && $add ['baktype'])) {
		printerror ( "EmptyBakFilesize", "history.go(-1)" );
	}
	// 目录名
	if (empty ( $add ['mypath'] )) {
		$add ['mypath'] = $dbname . "_" . date ( "YmdHis", time () );
	}
	DoMkdir ( $bakpath . "/" . $add ['mypath'] );
	// 生成说明文件
	$readme = $add ['readme'];
	$rfile = $bakpath . "/" . $add ['mypath'] . "/readme.txt";
	$readme .= "\r\n\r\nBaktime: " . date ( "Y-m-d H:i:s", time () );
	WriteFiletext_n ( $rfile, $readme );
	
	$b_table = "";
	$d_table = "";
	for($i = 0; $i < $count; $i ++) {
		$b_table .= $tablename [$i] . ",";
		$d_table .= "\$tb[" . $tablename [$i] . "]=0;\r\n";
	}
	// 去掉最后一个,
	$b_table = substr ( $b_table, 0, strlen ( $b_table ) - 1 );
	$bakstru = ( int ) $add ['bakstru'];
	$bakstrufour = ( int ) $add ['bakstrufour'];
	$beover = ( int ) $add ['beover'];
	$waitbaktime = ( int ) $add ['waitbaktime'];
	$bakdatatype = ( int ) $add ['bakdatatype'];
	if ($add ['insertf'] == 'insert') {
		$insertf = 'insert';
	} else {
		$insertf = 'replace';
	}
	if ($db_version == '4.0' && $add ['dbchar'] == 'auto') {
		$add ['dbchar'] = '';
	}
	$string = "<?php
	\$b_table=\"" . $b_table . "\";
	" . $d_table . "
	\$b_baktype=" . $add ['baktype'] . ";
	\$b_filesize=" . $add ['filesize'] . ";
	\$b_bakline=" . $add ['bakline'] . ";
	\$b_autoauf=" . $add ['autoauf'] . ";
	\$b_dbname=\"" . $dbname . "\";
	\$b_stru=" . $bakstru . ";
	\$b_strufour=" . $bakstrufour . ";
	\$b_dbchar=\"" . addslashes ( $add ['dbchar'] ) . "\";
	\$b_beover=" . $beover . ";
	\$b_insertf=\"" . addslashes ( $insertf ) . "\";
	\$b_autofield=\"," . addslashes ( $add ['autofield'] ) . ",\";
	\$b_bakdatatype=" . $bakdatatype . ";
	?>";
	$cfile = $bakpath . "/" . $add ['mypath'] . "/config.php";
	WriteFiletext_n ( $cfile, $string );
	if ($add ['baktype']) {
		$action = 'BakExeT';
	} else {
		$action = 'BakExe';
	}
	echo $fun_r ['StartToBak'] . "<script>self.location.href='bak.php?action=$action&t=0&s=0&p=0&mypath=$add[mypath]&waitbaktime=$waitbaktime';</script>";
	exit ();
}

/**
 * 执行备份(按文件大小)
 * 
 * @param unknown $t
 * @param unknown $s
 * @param unknown $p
 * @param unknown $mypath
 * @param unknown $alltotal
 * @param unknown $thenof
 * @param unknown $fnum
 * @param number $stime
 */
function BakExe($t, $s, $p, $mypath, $alltotal, $thenof, $fnum, $stime = 0) {
	global $dblink, $bakpath, $limittype, $fun_r;
	if (empty ( $mypath )) {
		printerror ( "ErrorUrl", "history.go(-1)" );
	}
	$path = $bakpath . "/" . $mypath;
	@include ($path . "/config.php");
	if (empty ( $b_table )) {
		printerror ( "ErrorUrl", "history.go(-1)" );
	}
	$waitbaktime = ( int ) $_GET ['waitbaktime'];
	if (empty ( $stime )) {
		$stime = time ();
	}
	$header = "<?php
require(\"../../inc/header.php\");
";
	$footer = "
require(\"../../inc/footer.php\");
?>";
	$btb = explode ( ",", $b_table );
	$count = count ( $btb );
	$t = ( int ) $t;
	$s = ( int ) $s;
	$p = ( int ) $p;
	// 备份完毕
	if ($t >= $count) {
		echo "<script>alert('" . $fun_r ['BakSuccess'] . "\\n\\n" . $fun_r ['TotalUseTime'] . ToChangeUseTime ( $stime ) . "');self.location.href='change_db.php';</script>";
		exit ();
	}
	$dumpsql = ReturnVer ();
	// 选择数据库
	$u = $dblink->query ( "use `$b_dbname`" );
	// 编码
	if ($b_dbchar == 'auto') {
		if (empty ( $s )) {
			$status_r = GetTotal ( $b_dbname, $btb [$t] );
			$collation = GetSetChar ( $status_r ['Collation'] );
			DoSetDbChar ( $collation );
			// 总记录数
			$num = $limittype ? - 1 : $status_r ['Rows'];
		} else {
			$collation = $_GET ['collation'];
			DoSetDbChar ( $collation );
			$num = ( int ) $alltotal;
		}
		$dumpsql .= ReturnSetNames ( $collation );
	} else {
		DoSetDbChar ( $b_dbchar );
		if (empty ( $s )) {
			// 总记录数
			if ($limittype) {
				$num = - 1;
			} else {
				$status_r = GetTotal ( $b_dbname, $btb [$t] );
				$num = $status_r ['Rows'];
			}
		} else {
			$num = ( int ) $alltotal;
		}
	}
	// 备份数据库结构
	if ($b_stru && empty ( $s )) {
		$dumpsql .= Returnstru ( $btb [$t], $b_strufour );
	}
	$sql = $dblink->query ( "select * from `" . $btb [$t] . "` limit $s,$num" );
	// 取得字段数
	if (empty ( $fnum )) {
		$return_fr = ReturnTbfield ( $b_dbname, $btb [$t], $b_autofield );
		$fieldnum = $return_fr ['num'];
		$noautof = $return_fr ['autof'];
	} else {
		$fieldnum = $fnum;
		$noautof = $thenof;
	}
	// 完整插入
	$inf = '';
	if ($b_beover == 1) {
		$inf = '(' . ReturnInTbfield ( $b_dbname, $btb [$t] ) . ')';
	}
	// 十六进制
	$hexf = '';
	if ($b_bakdatatype == 1) {
		$hexf = ReturnInStrTbfield ( $b_dbname, $btb [$t] );
	}
	$b = 0;
	while ( ! ! $r = $dblink->fetch ( $sql ) ) {
		$b = 1;
		$s ++;
		$dumpsql .= "E_D(\"" . $b_insertf . " into `" . $btb [$t] . "`" . $inf . " values(";
		$first = 1;
		for($i = 0; $i < $fieldnum; $i ++) {
			// 首字段
			if (empty ( $first )) {
				$dumpsql .= ',';
			} else {
				$first = 0;
			}
			$myi = $i + 1;
			if (! isset ( $r [$i] ) || strstr ( $noautof, ',' . $myi . ',' )) {
				$dumpsql .= 'NULL';
			} else {
				$dumpsql .= ReSqlFtext ( $r [$i], $b_bakdatatype, $myi, $hexf );
			}
		}
		$dumpsql .= ");\");\r\n";
		// 是否超过限制
		if (strlen ( $dumpsql ) >= $b_filesize * 1024) {
			$p ++;
			$sfile = $path . "/" . $btb [$t] . "_" . $p . ".php";
			$dumpsql = $header . $dumpsql . $footer;
			WriteFiletext_n ( $sfile, $dumpsql );
			$dblink->free ( $sql );
			// echo $fun_r['BakOneDataSuccess'].EchoBakSt($btb[$t],$count,$t,$num,$s)."<script>self.location.href='bak.php?action=BakExe&s=$s&p=$p&t=$t&mypath=$mypath&alltotal=$num&thenof=$noautof&fieldnum=$fieldnum&stime=$stime';</script>";
			
			echo "<meta http-equiv=\"refresh\" content=\"" . $waitbaktime . ";url=bak.php?action=BakExe&s=$s&p=$p&t=$t&mypath=$mypath&alltotal=$num&thenof=$noautof&fieldnum=$fieldnum&stime=$stime&waitbaktime=$waitbaktime&collation=$collation\">" . $fun_r ['BakOneDataSuccess'] . EchoBakSt ( $btb [$t], $count, $t, $num, $s );
			exit ();
		}
	}
	// 最后一个备份
	if (empty ( $p ) || $b == 1) {
		$p ++;
		$sfile = $path . "/" . $btb [$t] . "_" . $p . ".php";
		$dumpsql = $header . $dumpsql . $footer;
		WriteFiletext_n ( $sfile, $dumpsql );
	}
	RepFilenum ( $p, $btb [$t], $path );
	$t ++;
	$dblink->free ( $sql );
	// 进入下一个表
	// echo $fun_r['OneTableBakSuccOne'].$btb[$t].$fun_r['OneTableBakSuccTwo']."<script>self.location.href='bak.php?action=BakExe&s=0&p=0&t=$t&mypath=$mypath&stime=$stime';</script>";
	
	echo "<meta http-equiv=\"refresh\" content=\"" . $waitbaktime . ";url=bak.php?action=BakExe&s=0&p=0&t=$t&mypath=$mypath&stime=$stime&waitbaktime=$waitbaktime\">" . $fun_r ['OneTableBakSuccOne'] . $btb [$t - 1] . $fun_r ['OneTableBakSuccTwo'];
	exit ();
}

/**
 * 执行备份（按记录）
 * 
 * @param unknown $t
 * @param unknown $s
 * @param unknown $p
 * @param unknown $mypath
 * @param unknown $alltotal
 * @param unknown $thenof
 * @param unknown $fnum
 * @param string $auf
 * @param number $aufval
 * @param number $stime
 */
function BakExeT($t, $s, $p, $mypath, $alltotal, $thenof, $fnum, $auf = '', $aufval = 0, $stime = 0) {
	global $dblink, $bakpath, $limittype, $fun_r;
	if (empty ( $mypath )) {
		printerror ( "ErrorUrl", "history.go(-1)" );
	}
	$path = $bakpath . "/" . $mypath;
	@include ($path . "/config.php");
	if (empty ( $b_table )) {
		printerror ( "ErrorUrl", "history.go(-1)" );
	}
	$waitbaktime = ( int ) $_GET ['waitbaktime'];
	if (empty ( $stime )) {
		$stime = time ();
	}
	$header = "<?php
require(\"../../inc/header.php\");
";
	$footer = "
require(\"../../inc/footer.php\");
?>";
	$btb = explode ( ",", $b_table );
	$count = count ( $btb );
	$t = ( int ) $t;
	$s = ( int ) $s;
	$p = ( int ) $p;
	// 备份完毕
	if ($t >= $count) {
		echo "<script>alert('" . $fun_r ['BakSuccess'] . "\\n\\n" . $fun_r ['TotalUseTime'] . ToChangeUseTime ( $stime ) . "');self.location.href='change_db.php';</script>";
		exit ();
	}
	$dumpsql = ReturnVer ();
	// 选择数据库
	$u = $dblink->query ( "use `$b_dbname`" );
	// 编码
	if ($b_dbchar == 'auto') {
		if (empty ( $s )) {
			$status_r = GetTotal ( $b_dbname, $btb [$t] );
			$collation = GetSetChar ( $status_r ['Collation'] );
			DoSetDbChar ( $collation );
			// 总记录数
			$num = $limittype ? - 1 : $status_r ['Rows'];
		} else {
			$collation = $_GET ['collation'];
			DoSetDbChar ( $collation );
			$num = ( int ) $alltotal;
		}
		$dumpsql .= ReturnSetNames ( $collation );
	} else {
		DoSetDbChar ( $b_dbchar );
		if (empty ( $s )) {
			// 总记录数
			if ($limittype) {
				$num = - 1;
			} else {
				$status_r = GetTotal ( $b_dbname, $btb [$t] );
				$num = $status_r ['Rows'];
			}
		} else {
			$num = ( int ) $alltotal;
		}
	}
	// 备份数据库结构
	if ($b_stru && empty ( $s )) {
		$dumpsql .= Returnstru ( $btb [$t], $b_strufour );
	}
	// 取得字段数
	if (empty ( $fnum )) {
		$return_fr = ReturnTbfield ( $b_dbname, $btb [$t], $b_autofield );
		$fieldnum = $return_fr ['num'];
		$noautof = $return_fr ['autof'];
		$auf = $return_fr ['auf'];
	} else {
		$fieldnum = $fnum;
		$noautof = $thenof;
	}
	// 自动识别自增项
	$aufval = ( int ) $aufval;
	if ($b_autoauf == 1 && $auf) {
		$sql = $dblink->query ( "select * from `" . $btb [$t] . "` where " . $auf . ">" . $aufval . " order by " . $auf . " limit $b_bakline" );
	} else {
		$sql = $dblink->query ( "select * from `" . $btb [$t] . "` limit $s,$b_bakline" );
	}
	// 完整插入
	$inf = '';
	if ($b_beover == 1) {
		$inf = '(' . ReturnInTbfield ( $b_dbname, $btb [$t] ) . ')';
	}
	// 十六进制
	$hexf = '';
	if ($b_bakdatatype == 1) {
		$hexf = ReturnInStrTbfield ( $b_dbname, $btb [$t] );
	}
	$b = 0;
	while ( $r = $dblink->fetch ( $sql ) ) {
		if ($auf) {
			$lastaufval = $r [$auf];
		}
		$b = 1;
		$s ++;
		$dumpsql .= "E_D(\"" . $b_insertf . " into `" . $btb [$t] . "`" . $inf . " values(";
		$first = 1;
		for($i = 0; $i < $fieldnum; $i ++) {
			// 首字段
			if (empty ( $first )) {
				$dumpsql .= ',';
			} else {
				$first = 0;
			}
			$myi = $i + 1;
			if (! isset ( $r [$i] ) || strstr ( $noautof, ',' . $myi . ',' )) {
				$dumpsql .= 'NULL';
			} else {
				$dumpsql .= ReSqlFtext ( $r [$i], $b_bakdatatype, $myi, $hexf );
			}
		}
		$dumpsql .= ");\");\r\n";
	}
	if (empty ( $b )) {
		// 最后一个备份
		if (empty ( $p )) {
			$p ++;
			$sfile = $path . "/" . $btb [$t] . "_" . $p . ".php";
			$dumpsql = $header . $dumpsql . $footer;
			WriteFiletext_n ( $sfile, $dumpsql );
		}
		RepFilenum ( $p, $btb [$t], $path );
		$t ++;
		$dblink->free ( $sql );
		// 进入下一个表
		// echo $fun_r['OneTableBakSuccOne'].$btb[$t].$fun_r['OneTableBakSuccTwo']."<script>self.location.href='bak.php?action=BakExeT&s=0&p=0&t=$t&mypath=$mypath&stime=$stime';</script>";
		
		echo "<meta http-equiv=\"refresh\" content=\"" . $waitbaktime . ";url=bak.php?action=BakExeT&s=0&p=0&t=$t&mypath=$mypath&stime=$stime&waitbaktime=$waitbaktime\">" . $fun_r ['OneTableBakSuccOne'] . $btb [$t - 1] . $fun_r ['OneTableBakSuccTwo'];
		exit ();
	}
	// 进入下一组
	$p ++;
	$sfile = $path . "/" . $btb [$t] . "_" . $p . ".php";
	$dumpsql = $header . $dumpsql . $footer;
	WriteFiletext_n ( $sfile, $dumpsql );
	$dblink->free ( $sql );
	// echo $fun_r['BakOneDataSuccess'].EchoBakSt($btb[$t],$count,$t,$num,$s)."<script>self.location.href='bak.php?action=BakExeT&s=$s&p=$p&t=$t&mypath=$mypath&alltotal=$num&thenof=$noautof&fieldnum=$fieldnum&auf=$auf&aufval=$lastaufval&stime=$stime';</script>";
	
	echo "<meta http-equiv=\"refresh\" content=\"" . $waitbaktime . ";url=bak.php?action=BakExeT&s=$s&p=$p&t=$t&mypath=$mypath&alltotal=$num&thenof=$noautof&fieldnum=$fieldnum&auf=$auf&aufval=$lastaufval&stime=$stime&waitbaktime=$waitbaktime&collation=$collation\">" . $fun_r ['BakOneDataSuccess'] . EchoBakSt ( $btb [$t], $count, $t, $num, $s );
	exit ();
}

/**
 * 输出备份进度条
 *
 * @param unknown $tbname
 * @param unknown $tbnum
 * @param unknown $tb
 * @param unknown $rnum
 * @param unknown $r
 */
function EchoBakSt($tbname, $tbnum, $tb, $rnum, $r) {
	$table = ($tb + 1) . '/' . $tbnum;
	$record = $r;
	if ($rnum != - 1) {
		$record = $r . '/' . $rnum;
	}
	<<<Eof
<br>
<br>
<table width="90%" border="0" align="center" cellpadding="3"
	cellspacing="1">
	<tr>
		<td height="25">Table Name&nbsp;:&nbsp;<b><?php echo $tbname;?></b></td>
	</tr>
	<tr>
		<td height="25">Table&nbsp;:&nbsp;<b><?php echo $table;?></b></td>
	</tr>
	<tr>
		<td height="25">Record&nbsp;:&nbsp;<b><?php echo $record;?></b></td>
	</tr>
</table>
<br>
<br>
Eof;
}

/**
 * 输出恢复进度条
 *
 * @param unknown $tbname
 * @param unknown $tbnum
 * @param unknown $tb
 * @param unknown $pnum
 * @param unknown $p
 */
function EchoReDataSt($tbname, $tbnum, $tb, $pnum, $p) {
	$table = ($tb + 1) . '/' . $tbnum;
	$record = $p . '/' . $pnum;
	<<<Eof
<br>
<br>
<table width="90%" border="0" align="center" cellpadding="3"
	cellspacing="1">
	<tr>
		<td height="25">Table Name&nbsp;:&nbsp;<b><?php echo $tbname;?></b></td>
	</tr>
	<tr>
		<td height="25">Table&nbsp;:&nbsp;<b><?php echo $table;?></b></td>
	</tr>
	<tr>
		<td height="25">File&nbsp;:&nbsp;<b><?php echo $record;?></b></td>
	</tr>
</table>
<br>
<br>
Eof;
}

/**
 * 取得表记录数
 *
 * @param unknown $dbname
 * @param unknown $tbname
 * @return multitype:
 */
function GetTotal($dbname, $tbname) {
	global $dblink;
	/*
	 * $tr=$dblink->fetch1("select count(*) AS total from ".$btb[$t]); $num=$tr[total];
	 */
	$tr = $dblink->fetch1 ( "SHOW TABLE STATUS LIKE '" . $tbname . "';" );
	return $tr;
}

/**
 * 返回字符集set
 *
 * @param unknown $char
 * @return string Ambigous
 */
function GetSetChar($char) {
	global $dblink;
	if (empty ( $char )) {
		return '';
	}
	$r = $dblink->fetch1 ( "SHOW COLLATION LIKE '" . $char . "';" );
	return $r ['Charset'];
}

/**
 * 返回表字段信息
 *
 * @param unknown $dbname
 * @param unknown $tbname
 * @param unknown $autofield
 * @return string
 */
function ReturnTbfield($dbname, $tbname, $autofield) {
	global $dblink;
	$sql = $dblink->query ( "SHOW FIELDS FROM `" . $tbname . "`" );
	$i = 0; // 字段数
	$autof = ","; // 去除自增字段列表
	$f = ''; // 自增字段名
	while ( ! ! $r = $dblink->fetch ( $sql ) ) {
		$i ++;
		if (strstr ( $autofield, "," . $tbname . "." . $r ['Field'] . "," )) {
			$autof .= $i . ",";
		}
		if ($r ['Extra'] == 'auto_increment') {
			$f = $r ['Field'];
		}
	}
	$return_r ['num'] = $i;
	$return_r ['autof'] = $autof;
	$return_r ['auf'] = $f;
	return $return_r;
}

/**
 * 返回插入字段
 * 
 * @param unknown $dbname
 * @param unknown $tbname
 * @return string
 */
function ReturnInTbfield($dbname, $tbname) {
	global $dblink;
	$sql = $dblink->query ( "SHOW FIELDS FROM `" . $tbname . "`" );
	$f = '';
	$dh = '';
	while ( $r = $dblink->fetch ( $sql ) ) {
		$f .= $dh . '`' . $r ['Field'] . '`';
		$dh = ',';
	}
	return $f;
}

/**
 * 返回字符字段
 * 
 * @param unknown $dbname
 * @param unknown $tbname
 * @return string
 */
function ReturnInStrTbfield($dbname, $tbname) {
	global $dblink;
	$sql = $dblink->query ( "SHOW FIELDS FROM `" . $tbname . "`" );
	$i = 0;
	$f = '';
	$dh = '';
	while ( ! ! $r = $dblink->fetch ( $sql ) ) {
		$i ++;
		if (! (stristr ( $r [Type], 'char' ) || stristr ( $r [Type], 'text' ))) {
			continue;
		}
		$f .= $dh . $i;
		$dh = ',';
	}
	if ($f) {
		$f = ',' . $f . ',';
	}
	return $f;
}

/**
 * 字符过滤
 * 
 * @param unknown $str
 * @return mixed
 */
function escape_str($str) {
	$str = mysql_escape_string ( $str );
	$str = str_replace ( '\\\'', '\'\'', $str );
	$str = str_replace ( "\\\\", "\\\\\\\\", $str );
	$str = str_replace ( '$', '\$', $str );
	return $str;
}

/**
 * 返回字段内容
 * 
 * @param unknown $str
 * @param unknown $bakdatatype
 * @param unknown $i
 * @param unknown $tbstrf
 * @return string
 */
function ReSqlFtext($str, $bakdatatype, $i, $tbstrf) {
	if ($bakdatatype == 1 && ! empty ( $str ) && strstr ( $tbstrf, ',' . $i . ',' )) {
		$restr = '0x' . bin2hex ( $str );
	} else {
		$restr = '\'' . escape_str ( $str ) . '\'';
	}
	return $restr;
}

/**
 * 替换文件数
 * 
 * @param unknown $p
 * @param unknown $table
 * @param unknown $path
 */
function RepFilenum($p, $table, $path) {
	if (empty ( $p )) {
		$p = 0;
	}
	$file = $path . "/config.php";
	$text = ReadFiletext ( $file );
	$rep1 = "\$tb[" . $table . "]=0;";
	$rep2 = "\$tb[" . $table . "]=" . $p . ";";
	$text = str_replace ( $rep1, $rep2, $text );
	WriteFiletext_n ( $file, $text );
}

/**
 * 执行SQL
 * 
 * @param unknown $sql
 */
function E_D($sql) {
	global $dblink;
	$dblink->query ( $sql );
}

/**
 * 建立表
 * 
 * @param unknown $sql
 */
function E_C($sql) {
	global $dblink;
	$dblink->query ( AddDbchar ( $sql ) );
}

/**
 * 转为Mysql4.0格式
 * 
 * @param unknown $query
 * @return unknown string
 */
function ToMysqlFour($query) {
	$exp = "ENGINE=";
	if (! strstr ( $query, $exp )) {
		return $query;
	}
	$exp1 = " ";
	$r = explode ( $exp, $query );
	// 取得表类型
	$r1 = explode ( $exp1, $r [1] );
	$returnquery = $r [0] . "TYPE=" . $r1 [0];
	return $returnquery;
}

/**
 * 返回数据库结构
 * 
 * @param unknown $table
 * @param unknown $strufour
 * @return string
 */
function Returnstru($table, $strufour) {
	global $dblink;
	$dumpsql = '';
	$dumpsql .= "E_D(\"DROP TABLE IF EXISTS `" . $table . "`;\");\r\n";
	// 设置引号
	$usql = $dblink->query ( "SET SQL_QUOTE_SHOW_CREATE=1;" );
	// 数据表结构
	$r = $dblink->fetch1 ( "SHOW CREATE TABLE `$table`;" );
	$create = str_replace ( "\"", "\\\"", $r [1] );
	// 转为4.0格式
	if ($strufour) {
		$create = ToMysqlFour ( $create );
	}
	$dumpsql .= "E_C(\"" . $create . "\");\r\n";
	return $dumpsql;
}

/**
 * 返回设置编码
 * 
 * @param unknown $char
 * @return string
 */
function ReturnSetNames($char) {
	if (empty ( $char )) {
		return '';
	}
	$dumpsql = "DoSetDbChar('" . $char . "');\r\n";
	return $dumpsql;
}

/**
 * 去除字段中的编码
 * 
 * @param unknown $sql
 * @return mixed
 */
function ReplaceFieldChar($sql) {
	global $db_version;
	if ($db_version == '4.0' && strstr ( $sql, ' character set ' )) {
		$preg_str = "/ character set (.+?) collate (.+?) /is";
		$sql = preg_replace ( $preg_str, ' ', $sql );
	}
	return $sql;
}

/**
 * 加编码
 * 
 * @param unknown $sql
 * @return Ambigous <mixed, unknown>
 */
function AddDbchar($sql) {
	global $db_version, $db_char, $b_dbchar;
	// 加编码
	if ($db_version >= '4.1' && ! strstr ( $sql, 'ENGINE=' ) && ($db_char || $b_dbchar) && $b_dbchar != 'auto') {
		$dbcharset = $b_dbchar ? $b_dbchar : $db_char;
		$sql = DoCreateTable ( $sql, $db_version, $dbcharset );
	} elseif ($db_version == '4.0' && strstr ( $sql, 'ENGINE=' )) {
		$sql = ToMysqlFour ( $sql );
	}
	// 去除字段中的编码
	$sql = ReplaceFieldChar ( $sql );
	return $sql;
}

/**
 * 建表
 * 
 * @param unknown $sql
 * @param unknown $mysqlver
 * @param unknown $dbcharset
 * @return string
 */
function DoCreateTable($sql, $mysqlver, $dbcharset) {
	$type = strtoupper ( preg_replace ( "/^\s*CREATE TABLE\s+.+\s+\(.+?\).*(ENGINE|TYPE)\s*=\s*([a-z]+?).*$/isU", "\\2", $sql ) );
	$type = in_array ( $type, array (
			'MYISAM',
			'HEAP' 
	) ) ? $type : 'MYISAM';
	return preg_replace ( "/^\s*(CREATE TABLE\s+.+\s+\(.+?\)).*$/isU", "\\1", $sql ) . ($mysqlver >= '4.1' ? " ENGINE=$type DEFAULT CHARSET=$dbcharset" : " TYPE=$type");
}

/**
 * 返回版权信息
 * 
 * @return string
 */
function ReturnVer() {
	$string = "
/*
		SoftName : TBak Version 2015
		Author   : Fasttop
		Copyright: Powered by fasttop.top
*/

";
	return $string;
}

/**
 * 导入数据
 * 
 * @param unknown $add
 * @param unknown $mypath
 */
function ReData($add, $mypath) {
	global $dblink, $bakpath;
	if (empty ( $mypath ) || empty ( $add [mydbname] )) {
		printerror ( "EmptyReData", "history.go(-1)" );
	}
	$path = $bakpath . "/" . $mypath;
	if (! file_exists ( $path )) {
		printerror ( "PathNotExists", "history.go(-1)" );
	}
	@include ($path . "/config.php");
	if (empty ( $b_table )) {
		printerror ( "FailBakVar", "history.go(-1)" );
	}
	$waitbaktime = ( int ) $add ['waitbaktime'];
	$btb = explode ( ",", $b_table );
	$nfile = $path . "/" . $btb [0] . "_1.php?t=0&p=0&mydbname=$add[mydbname]&mypath=$mypath&waitbaktime=$waitbaktime";
	Header ( "Location:$nfile" );
	exit ();
}

/**
 * 运行SQL
 * 
 * @param unknown $sql
 * @param unknown $mydbchar
 * @param unknown $mydbver
 */
function DoRunQuery($sql, $mydbchar, $mydbver) {
	$sql = str_replace ( "\r", "\n", $sql );
	$ret = array ();
	$num = 0;
	foreach ( explode ( ";\n", trim ( $sql ) ) as $query ) {
		$queries = explode ( "\n", trim ( $query ) );
		foreach ( $queries as $query ) {
			@$ret [$num] .= $query [0] == '#' || $query [0] . $query [1] == '--' ? '' : $query;
		}
		$num ++;
	}
	unset ( $sql );
	foreach ( $ret as $query ) {
		$query = trim ( $query );
		if ($query) {
			if (substr ( $query, 0, 12 ) == 'CREATE TABLE') {
				mysql_query ( DoCreateTable ( $query, $mydbver, $mydbchar ) ) or die ( mysql_error () . "<br>" . $query );
			} else {
				mysql_query ( $query ) or die ( mysql_error () . "<br>" . $query );
			}
		}
	}
}

/**
 * 上传文件
 * 
 * @param unknown $file
 * @param unknown $newfile
 * @return unknown
 */
function DoTranFile($file, $newfile) {
	$cp = @move_uploaded_file ( $file, $newfile );
	return $cp;
}

/**
 * 函数是否存在
 * 
 * @param unknown $fun
 * @return number
 */
function HaveFun($fun) {
	if (function_exists ( $fun )) {
		$word = 1;
	} else {
		$word = 0;
	}
	return $word;
}

/**
 * 是否支持ICONV库
 * 
 * @return number
 */
function GetIconv() {
	$can = HaveFun ( "iconv" );
	return $can;
}

/**
 * 编码转换
 * 
 * @param unknown $str
 * @param unknown $oldchar
 * @param unknown $newchar
 * @return unknown string
 */
function ChangeChar($str, $oldchar, $newchar) {
	// 是否支持iconv
	if (! HaveFun ( "iconv" )) {
		return $str;
	}
	if (! empty ( $newchar )) {
		$str = iconv ( $oldchar, $newchar, $str );
	}
	return $str;
}

/**
 * 返回语言目录
 * 
 * @return string
 */
function ReturnLang() {
	global $ebaklang, $langcharr;
	$count = count ( $langcharr );
	$l = '';
	for($i = 0; $i < $count; $i ++) {
		$f = explode ( ',', $langcharr [$i] );
		if (! file_exists ( 'lang/' . $f [0] )) {
			continue;
		}
		$select = '';
		if ($f [0] == $ebaklang) {
			$select = ' selected';
		}
		$l .= "<option value='" . $i . "'" . $select . ">" . $f [2] . " (" . $f [1] . ")</option>";
	}
	return $l;
}

/**
 * 返回数据库编码列表
 * 
 * @param unknown $dbchar
 * @return string
 */
function ReturnDbCharList($dbchar) {
	global $dbcharr;
	$count = count ( $dbcharr );
	$c = '';
	for($i = 0; $i < $count; $i ++) {
		$select = '';
		if ($dbcharr [$i] == $dbchar) {
			$select = ' selected';
		}
		$c .= "<option value='" . $dbcharr [$i] . "'" . $select . ">" . $dbcharr [$i] . "</option>";
	}
	return $c;
}

/**
 * 设置转向备份页面
 * 
 * @param unknown $file
 */
function SetGotoBak($file) {
	if (strstr ( $file, '.' ) || strstr ( $file, '/' ) || strstr ( $file, "\\" )) {
		printerror ( "FailSetSavename", "history.go(-1)" );
	}
	@include ('setsave/' . $file);
	Header ( "Location:change_table.php?mydbname=$dmydbname&savefilename=$file" );
	exit ();
}

/**
 * 转向恢复页面
 * 
 * @param unknown $path
 */
function PathGotoRedata($path) {
	global $bakpath;
	if (strstr ( $path, ".." )) {
		printerror ( "NotChangeDelPath", "history.go(-1)" );
	}
	if (! trim ( $path )) {
		printerror ( "NotChangeDelPath", "history.go(-1)" );
	}
	$repath = $bakpath . "/" . $path;
	if (! file_exists ( $repath )) {
		printerror ( "DelPathNotExists", "history.go(-1)" );
	}
	@include $repath . '/config.php';
	Header ( "Location:re_data.php?mydbname=$b_dbname&mypath=$path" );
	exit ();
}

/**
 * 替换字符
 * 
 * @param unknown $text
 * @param unknown $exp
 * @param number $enews
 * @return string
 */
function RepInfoZZ($text, $exp, $enews = 0) {
	$text = str_replace ( "*", "(.*?)", $text );
	$text = str_replace ( "[!--" . $exp . "--]", "(.*?)", $text );
	// $text=str_replace("\\","\\\\",$text);
	// $text=str_replace("^","\^",$text);
	// $text=str_replace("\"","\"",$text);
	$text = str_replace ( "/", "\/", $text );
	$text = "/" . $text . "/is";
	return $text;
}

/**
 * 正则替换信息
 * 
 * @param unknown $oldword
 * @param unknown $newword
 * @param unknown $text
 * @return mixed
 */
function DoRepFiletextZz($oldword, $newword, $text) {
	$zztext = RepInfoZZ ( $oldword, "tbak-fasttop.mcyami", 0 );
	$text = preg_replace ( $zztext, $newword, $text );
	return $text;
}